//
//  ZQVistorView.swift
//  Sina Blog
//
//  Created by 张强 on 16/5/11.
//  Copyright © 2016年 张强. All rights reserved.
//

import UIKit
import SnapKit
class ZQVistorView: UIView {
    
    var callBack: (() -> ())?
    
    private lazy var circleView: UIImageView = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_image_smallicon"))
    private lazy var maskImageView: UIImageView = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var iconView: UIImageView = UIImageView.init(image: UIImage.init(named: "visitordiscover_feed_image_house"))
    
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.textAlignment = .Center
        label.numberOfLines = 0
        
        return label
        
    }()
    
    private lazy var registerButton: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), forState: .Normal)
        
        btn.setTitleColor(UIColor.darkTextColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.addTarget(self, action: #selector(registBtnClick), forControlEvents: .TouchUpInside)
        
        return btn
        
        
    }()
    
    
    private lazy var loginBUtton: UIButton = {
        
        let btn = UIButton()
        btn.setTitle("登陆", forState: .Normal)
        btn.setTitleColor(UIColor.darkTextColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        btn.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.addTarget(self, action: #selector(loginBtnClick), forControlEvents: .TouchUpInside)
        
        
        
        return btn
        
    }()
    
    func setInfo(title: String?, imgName: String?) {
        if let t = title, imageName = imgName {
            messageLabel.text = t
            iconView.image = UIImage.init(named: imageName)
            circleView.hidden = true
        }else {
        
            circleView.hidden = false
            playAnimation()
        
        }
    }
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
       
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(loginBUtton)
        addSubview(registerButton)
        addSubview(messageLabel)
        addSubview(circleView)
        addSubview(maskImageView)
        addSubview(iconView)
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        circleView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
            
        }
//
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(circleView)
            
        }
//
        iconView.snp_makeConstraints { (make) in
            make.center.equalTo(circleView)
            
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.topMargin.equalTo(circleView.snp_bottom).offset(10)
            make.centerX.equalTo(circleView)
            make.size.equalTo(CGSizeMake(224, 48))
            
        }
        
        registerButton.snp_makeConstraints { (make) in
            make.topMargin.equalTo(messageLabel.snp_bottom).offset(10)
            make.leading.equalTo(messageLabel)
            make.size.equalTo(CGSizeMake(100, 35))
            
        }
        
        loginBUtton.snp_makeConstraints { (make) in
            make.topMargin.equalTo(messageLabel.snp_bottom).offset(10)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
            
        }
        
        
        
    }
    
    
    
    
    @objc private func loginBtnClick() {
        callBack!()
        
    }
    
    
    
    @objc private func registBtnClick(){
        
        callBack!()
    }
    
    
    private func playAnimation() {
        let animation = CABasicAnimation(keyPath:"transform.rotation")
        animation.toValue = M_PI * 2
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        animation.removedOnCompletion = false
        circleView.layer.addAnimation(animation, forKey: nil)
        
    }
    
    
    
    
}
